package cn.lsnu.modules.oa.controller;

import cn.lsnu.common.interfacetools.interfacecontroller.BaseController;
import cn.lsnu.common.interfacetools.interfaceentity.Page;
import cn.lsnu.modules.oa.entity.UserEntity;
import cn.lsnu.modules.oa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/oa/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public void save(UserEntity  userEntity) throws Exception {
        userService.save(userEntity);
    }

    @RequestMapping(value = "/lists")
    public
        @ResponseBody List<UserEntity>
        lists(UserEntity  userEntity, Model model) throws Exception {
       return userService.findList(userEntity);
    }




    @RequestMapping("/list")
    public String list(@RequestParam("menuId")String id, Model model) throws  Exception{
        //每个-list页面的id
        model.addAttribute("pageId",id);
        return "/webpage/modules/oa/admin-list";
    }

    @RequestMapping("/listjson")
    public @ResponseBody Page<UserEntity>
                        listJson(UserEntity userEntity) throws Exception {
        Page<UserEntity> lists = userService.findPage(userEntity);
        return  lists;
    }


    @RequestMapping("/updmsg")
    public String updMsg(String id,Model model) throws Exception {

        model.addAttribute("upduser",userService.get(id));

        return "/webpage/modules/oa/admin-upd";
    }
    @RequestMapping("/upd")
    public void upd(UserEntity userEntity) throws Exception {
            userService.update(userEntity);

    }

    @RequestMapping("/del")
    public  @ResponseBody
            Map<String,String> delete(UserEntity userEntity) throws Exception{
        Map<String,String> msg = new HashMap<String,String>();
        userService.delete(userEntity);
        msg.put("msg","success");
        return  msg;
    }

    @RequestMapping("/updstatus")
    public  @ResponseBody
    Map<String,String> updStatus(UserEntity userEntity) throws Exception{
        Map<String,String> msg = new HashMap<String,String>();
        userService.updateStatus(userEntity);
        msg.put("msg","success");
        return  msg;
    }

}
